package wumpus;

import java.io.*;
import java.util.*;

/** Implements a human controlled agent. **/
public class BestAgent implements WumpusAgent
{
    int width, height;

    int breezy[][], smelly[][], glimmery[][];
    int agentx, agenty;

    public void init(int width, int height)
    {
        this.width = width;
        this.height = height;

        breezy = new int[height][width];
        smelly = new int[height][width];
        glimmery = new int[height][width];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                breezy[y][x] = -1;
                smelly[y][x] = -1;
                glimmery[y][x] = -1;
            }
        }
    }

    public String getMove(boolean breeze, boolean stench, boolean glimmer)
    {
        breezy[agenty][agentx] = breeze ? 1 : 0;
        smelly[agenty][agentx] = stench ? 1 : 0;
        glimmery[agenty][agentx] = glimmer ? 1 : 0;

        ////////////////////////////////////////////////////
        // Display playing board
        for (int x = 0; x < width; x++)
            System.out.printf("======");
        System.out.printf("=\n");

        for (int y = height-1; y >= 0; y--) {
            for (int x = 0; x < width; x++) {

                System.out.printf("| ");

                if (y==agenty && x==agentx)
                    System.out.printf("A");
                else
                    System.out.printf(" ");

                if (breezy[y][x] == -1)
                    System.out.printf("?");
                else if (breezy[y][x] == 0)
                    System.out.printf(" ");
                else
                    System.out.printf("B");

                if (smelly[y][x] == -1)
                    System.out.printf("?");
                else if (smelly[y][x] == 0)
                    System.out.printf(" ");
                else
                    System.out.printf("S");

                if (glimmery[y][x] == -1)
                    System.out.printf("?");
                else if (glimmery[y][x] == 0)
                    System.out.printf(" ");
                else
                    System.out.printf("G");
            }

            System.out.printf("|\n");
        }

        for (int x = 0; x < width; x++)
            System.out.printf("======");
        System.out.printf("=\n");

        ////////////////////////////////////////////////////
        // Get move
        System.out.printf("> ");

        try {
            String move = new BufferedReader(new InputStreamReader(System.in)).readLine();
            move = move.toUpperCase().trim();
            String toks[] = move.split("\\s+");

            // allow humans to just type 'n/s/e/w' to move.
            if (move.equals("N") || move.equals("S") || move.equals("E") || move.equals("W")) {
                move = "MOVE "+move;
                toks = move.split("\\s+");
            }

            if (toks[0].equals("MOVE")) {
                if (toks.length == 3) {
                    agentx += Integer.parseInt(toks[1]);
                    agenty += Integer.parseInt(toks[2]);
                } else if (toks[1].equals("N"))
                    agenty++;
                else if (toks[1].equals("S"))
                    agenty--;
                else if (toks[1].equals("E"))
                    agentx++;
                else if (toks[1].equals("W"))
                    agentx--;
            }

            return move;
        } catch (IOException ex) {
        }

        return "";
    }
}
